$(function(){
    var login_flag = true,login_count=0;
    $.mvalidateExtend({
        //验证手机号 密码  校验码
        username:{
            required : true,   
            descriptions:{
                required : '<div class="field-invalidmsg">请输入登录用户名</div>',
            }
        },
        pwd:{
            required : true,   
            descriptions:{
                required : '<div class="field-invalidmsg">请输入登录密码</div>',
            }
        }
    });
    $("#form-login").mvalidate({
        type:1,
        onKeyup:true,
        sendForm:true,
        firstInvalidFocus:false,
        conditional: {
            username: function() {
                return $("#uName").val() == 1;
            },
            pwd: function() {
                return $("#uPass").val() == 1;
            }
        },
        descriptions: {
            username: {
                conditional: '账号或密码错误'
            },
            pwd: {
                conditional: '账号或密码错误'
            }
        },
        valid:function(event,options){
            add_login_oper_listener();
            //点击提交按钮时,表单通过验证触发函数
            var uName = $.trim($("#uName").val());
            var uPass = $("#uPass").val();
            var url = $('#form-login').attr("data-href");
            login_flag = false;
            $("#form-login-btn").val("登录中...");
            $.ajax({
                type: "POST",
                url: url,
                async:true, 
                data: 'uName='+uName+'&uPass='+uPass+"&redirectUrl="+$("#redirectUrl").val(),
                success: function(resp){
                     resp = $.parseJSON(resp);
                     if(resp.reqStatus){
                        window.location.href = resp.url;
                     }else{
                        cancel_login_oper_listener();
                        $("#form-login-btn").val("重新登录");
                        $.mvalidateTip(resp.errorMsg);
                        return false;
                     }
                 }
            });
            return false;
        },
        invalid:function(event, status, options){
            //点击提交按钮时,表单未通过验证触发函数
        },
        eachField:function(event,status,options){
            //点击提交按钮时,表单每个输入域触发这个函数 this 执向当前表单输入域，是jquery对象
        },
        eachValidField:function(val){},
        eachInvalidField:function(event, status, options){},
    })
    
    function add_login_oper_listener(){
        if(login_count == 2){
            login_count = 0;
            $.mvalidateTip("操作过于频繁");return false;
        }
        if(!login_flag){
            login_count ++;
            $.mvalidateTip("正在处理登录操作");return false;
        }
    }
    function cancel_login_oper_listener(){
        login_count = 0;
        login_flag = true;
    }
});

